<?php
namespace Api\Controller;
use Think\Controller;
class AuthController extends CommonController {
    public function check(){
		$Auth = $this->isAuth();
		if($Auth == true)
		{
			$User = M ('User');
			$ret = $User->where("user_id='{$_SESSION['user_id']}'")->find();
			//echo $User->getLastsql();
			if($ret['mobile_no'] == '0'){
				$this->response(false,3,"未绑定手机号码",true);			
			}
			$data = array(
				'user_id' => $_SESSION['user_id'],
				'user_nickname' => $_SESSION['user_nickname'],
				'head_img' => $_SESSION['head_img'],
			);
			$this->response(true,1,$data,true);
		}else{
			$this->response(false,0,"未授权登录",true);
		}
	}
	public function bindMobile(){
		$mobile_no = I ('get.mobile2');
		$code = I ('get.code2');
		//判断短信验证
		//code 123456
		if($code != '123456'){
			echo "123";
			$this->response(false,0,"验证码错误",true);
		}
		$User = M ('User');
		$ret = $User->where("mobile_no='{$mobile_no}'")->find();
		//echo $User->getLastsql();
		//手机未注册
		$user_id = $_SESSION['user_id'];
		//echo $user_id ;
		if($ret == false){
			$data = array(
			'mobile_no' => $mobile_no
			);
			$res = $User->where("user_id='{$user_id}'")->save($data);
			$this->response(true,1,"绑定成功",true);
		}
		//手机被注册，合并数据
		//查询微博用户的数据
		$result = $User->where("user_id={$user_id}")->find();
		$data = array(
				'weibo_id' => $result['weibo_id'],
				'user_nickname' => $result['user_nickname'],
				'head_img' => $result['head_img']
			);
		$ret = $User->where("mobile_no='{$mobile_no}'")->save($data);
		
		if( $ret != false ){
			$result = $User->where("user_id='{$user_id}'")->delete();
			$_SESSION['user_id'] = $ret['user_id'];
			$_SESSION['user_nickname'] = $ret['user_nickname'];
			$_SESSION['head_img'] = $ret['head_img'];
			$this->response(true,1,"合并成功",true);	
		}	
		
	}
	public function loginout(){
		unset($_SESSION['user_id']);
		unset($_SESSION['user_nickname']);
		//unset($_SESSION['head_img']);
		$this->response(true,0,null,true);
	}
	public function login(){
		$user = I ('get.user');
		$password = I ('get.password');
		if( $user == false){
				$this->error("用户名不能为空");
			}
	    $User = D("User"); 
		$res = $User->where("user_nickname='{$user}'")->find();
		if( $res == false ){
			$this->response(false, 0, '不存在该用户');
		}
		$getpassword = $User->where("user_nickname='{$user}'")->getField('password');
		if( $password!= $getpassword){
			$this->response(false, 1,'密码错误',true);
		}	
		$_SESSION['user_id'] = $res['user_id'];
		$_SESSION['user_nickname'] = $res['user_nickname'];
		//$_SESSION['head_img'] = $res['head_img'];	
		$this->response(true, 2,'登录成功', true);
	}
}
